package T01_test_all.test10_Student;

public class T10_StudentDelet {
    public static void main(String[] args) {

        T10_Student[] arr = new T10_Student[3];

        T10_Student stu1 = new T10_Student(1, "xh", 18);
        T10_Student stu2 = new T10_Student(2, "lirong", 18);
        T10_Student stu3 = new T10_Student(3, "tudou", 18);

        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;

        int index = getIndex(arr,4);
//        System.out.println(index);
//        //判断是否存在
//        if(index >= 0){
//            arr[index] = null;
//        }else {
//            printArr(arr);
//        }
        //存在 age+1
        if(index >= 0){
            T10_Student stu = arr[index];
            int newAge = stu.getAge() + 1;
            stu.setAge(newAge);
            System.out.println(stu.getAge());
        }else {
            System.out.println("数据不存在");
            printArr(arr);
        }

    }

    /***
     * 查找 是否存在 索引ID
     * @param arr
     * @param id
     * @return
     */
    public static int getIndex(T10_Student[] arr, int id){
        for (int i = 0; i < arr.length; i++) {
            T10_Student stu = arr[i];
            if(stu != null){
                int sid = stu.getId();
                if(sid == id){
                    return i;
                }
            }
        }
        return -1;
    }

    /***
     * 遍历输出
     * @param arr
     */
    public static void printArr(T10_Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            T10_Student stu = arr[i];
            if(stu != null){
                System.out.println(stu.getId()+ " " + stu.getName() + " " + stu.getAge());
            }
        }
    }
}
